
#Time Estimate Constants
COM_PROCESSING_TIME = 1.0
CPFV_PROCESSING_TIME = 1.0
DIV_PROCESSING_TIME = 0.4
KYK_PROCESSING_TIME = 0.15
PVT_PROCESSING_TIME = 0.85
SWD_PROCESSING_TIME = 0.15
ALREADY_CACHED_TIME = .01

'''
Returns estimated processing time for a single mpa on a single fishing group
Called by estimate_time_for_mpa
'''
def estimated_time_for_fishing_group(group=None):
    from utilities import isCOM, isCPFV, isDIV, isKYK, isPVT, isSWD
    if isCOM(group):
        return COM_PROCESSING_TIME
    if isCPFV(group):
        return CPFV_PROCESSING_TIME
    if isDIV(group):
        return DIV_PROCESSING_TIME
    if isKYK(group):
        return KYK_PROCESSING_TIME
    if isPVT(group):
        return PVT_PROCESSING_TIME
    if isSWD(group):
        return SWD_PROCESSING_TIME
    return COM_PROCESSING_TIME + CPFV_PROCESSING_TIME + DIV_PROCESSING_TIME + KYK_PROCESSING_TIME + PVT_PROCESSING_TIME + SWD_PROCESSING_TIME

'''
Returns an estimated run time (on production level server) for a given list of mpas
Estimated times for each fishing group (Commercial, Recreational Kayak, etc) are determined individually
Called by estimate_processing_times
'''
def estimate_time_for_mpas(mpa_list, group=None):
    #estimate time for analysis...
    estimated_run_time = 0
    for mpa in mpa_list:
        estimated_run_time += estimate_time_for_mpa(mpa, group)
    return estimated_run_time

'''    
Returns estimated processing time for single mpa on a single fishing group, or for all fishing groups (if group is None)
Estimated time is based on whether the results have been cached already (and are up to date) or not
Called by estimate_time_for_mpas
'''
def estimate_time_for_mpa(mpa, group=None):
    from AnalysisCache import mpa_cache_is_available_and_current
    from utilities import nc_constants
    estimated_run_time = 0
    if group is None:
        groups = nc_constants.GROUPS.keys()
        for group in groups:
            if mpa_cache_is_available_and_current(mpa, group):
                estimated_run_time += ALREADY_CACHED_TIME
            else:
                estimated_run_time += estimated_time_for_fishing_group(group)
    else:
        if mpa_cache_is_available_and_current(mpa, group):
            estimated_run_time += ALREADY_CACHED_TIME
        else:
            estimated_run_time += estimated_time_for_fishing_group(group)
    return estimated_run_time
  
'''
Returns a more readable text version of a given time (time is a decimal number of minutes)
Called by estimate_processing_times 
''' 
def convert_time_to_text(time):
    if time < 2:
        text_time = 'just a couple of minutes'
    else:
        time = (int)(time + .5) 
        hours = time / 60
        minutes = time % 60
        if hours > 0:
            text_time = 'approximately %s hour(s) and %s minute(s)' % (hours, minutes)
        else:
            text_time = 'approximately %s minutes' % minutes
    return text_time

 
'''    
Returns a dictionary representing estimated processing times for each user/group
{'user': time, ...} or {'group': time, ...}
Called by views.adminUserAnalysis and views.adminGroupAnalysis
'''
def estimate_processing_times(user_ids=None, group_ids=None):
    from utilities import get_user_mpas, get_group_mpas
    if user_ids is not None:
        mpa_lists = get_user_mpas(user_ids)
    else:
        mpa_lists = get_group_mpas(group_ids)
    processing_times = {}
    total_time = 0
    for owner, mpa_list in mpa_lists.items():
        time = estimate_time_for_mpas(mpa_list)
        total_time += time
        text_time = convert_time_to_text(time)
        processing_times[owner] = text_time
    
    total_time = convert_time_to_text(total_time)
    return processing_times, total_time
